/*
 * ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is Mozilla Communicator client code, released
 * March 31, 1998.
 *
 * The Initial Developer of the Original Code is
 * Netscape Communications Corporation.
 * Portions created by the Initial Developer are Copyright (C) 1998-1999
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *   Daniel Veditz <dveditz@netscape.com>
 *   Samir Gehani <sgehani@netscape.com>
 *   Mitch Stoltz <mstoltz@netscape.com>
 *
 *   Lan Qiang <jameslan@gmail.com>
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK *****
 */

#ifndef nsZipEntryIndex_h__
#define nsZipEntryIndex_h__

#include "nsIZipReader.h"

/**
 * nsZipEntryItem
 */
struct nsZipEntryItem
{
	nsZipEntryItem*          hashNext;
	nsZipEntryItem*          next;
	nsZipEntryItem*          prev;
	PRUint32                 offset;
	PRUint32                 dataOffset;
	PRUint32                 size;           ///< size in original file
	PRUint32                 realSize;       ///< inflated size
	PRUint32                 itemCRC32;
	PRUint16                 time;
	PRUint16                 date;
	PRUint16                 mode;
	PRUint8                  compression;
	PRPackedBool             hasDataOffset : 1;
	PRPackedBool             isDirectory : 1; 

	/* Whether item is an actual zip entry or was generated as part of
	 * a real entry's path, e.g. foo/ in a zip containing only
	 * foo/a.txt and no foo/ entry is synthetic.
	 */
	PRPackedBool             isSynthetic : 1;
#if defined(XP_UNIX) || defined(XP_BEOS)
	PRPackedBool isSymlink : 1;
#endif
	char                     name[1]; // actually, bigger than 1
};

#define ZIP_BUCKETCOUNT          256
struct PRFileDesc;
/**
 * nsZipEntryIndex
 */
class nsZipEntryIndex
{
public:
	nsZipEntryIndex(PRFileDesc* aFd);
	~nsZipEntryIndex();

	nsresult ReadEntryIndex(PRUint32 *offsetCentralDir);
	nsresult WriteEntryIndex(PRUint32 offset);
	nsresult GetItem(const char* anEntryName, nsZipEntryItem **anItem);
	nsZipEntryItem* GetBegin() { return mFirstEntry; }
	nsresult InsertIntoHashTable(nsZipEntryItem *anItem);
	nsresult RemoveFromHashTable(const char* anEntryName);
	nsresult WriteLocalHeader(const nsZipEntryItem *anItem);
	PRUint32 GetEntryCount() { return mEntryCount; }

private:
	nsresult FindCentralDir(PRUint32* anOffset);
	nsresult ReadItem(nsZipEntryItem** anItem);
	nsresult WriteItem(nsZipEntryItem *item);
	void RemoveItem(nsZipEntryItem *item);

	PRFileDesc*              mFd;
	nsZipEntryItem*          mBuckets[ZIP_BUCKETCOUNT];
	nsZipEntryItem*          mFirstEntry;
	nsZipEntryItem*          mLastEntry;
	PRUint32                 mEntryCount;
	friend class nsZipEntryFind;
};

/** 
 * nsZipEntryFind 
 *
 * a helper class for nsZipEntryIndex, representing a search
 */
class nsZipEntryFind
{
public:
	nsZipEntryFind();
	~nsZipEntryFind();

	nsresult Init(nsZipEntryIndex* anIndex, const char* aPattern);
	nsresult FindNext(const char ** aResult);

private:
	nsZipEntryItem*          mItem;
	char*                    mPattern;
	PRPackedBool             mRegExp;

	void FindAvailable();

	//-- prevent copies and assignments
	nsZipEntryFind& operator=(const nsZipEntryFind& rhs);
	nsZipEntryFind(const nsZipEntryFind& rhs);
};

inline static PRUint32 le32_to_host(PRUint32 i)
{
	unsigned char * const v = (unsigned char*)&i;

	return (v[0]) | (v[1]<<8) | (v[2]<<16) | (v[3]<<24);
}

inline static PRUint16 le16_to_host(PRUint16 i)
{
	unsigned char * const v = (unsigned char*)&i;

	return (v[0]) | (v[1]<<8);
}

inline static PRUint32 host_to_le32(PRUint32 i)
{
	return le32_to_host(i);
}

inline static PRUint16 host_to_le16(PRUint16 i)
{
	return le16_to_host(i);
}

#endif
